/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import projeto.classes.ClienteAcessaDados;
import projeto.classes.ModeloCliente;
import projeto.classes.Util.Util;

/**
 *
 * @author Demetrio
 */
public class fCliente extends javax.swing.JInternalFrame {

     private ModeloCliente cli = new ModeloCliente();
    private ClienteAcessaDados cliDao = new ClienteAcessaDados();
    private ArrayList<ModeloCliente> lista;
    private br.com.fatecpg.mdi.MDI2 mdi;

    /**
     * Creates new form _Cliente
     */
    public fCliente() {
        initComponents();
    }
    
    public fCliente(br.com.fatecpg.mdi.MDI2 mdi, projeto.classes.ModeloUsuario user) {
        initComponents();
        this.mdi = mdi;
        
        //getUsuarioAdmin igual a 0 quer dizer que ele é um usuario normal
        if (user.getUsuarioAdmin() == 0) {
            btnExcluir.setVisible(false);
        }
        
        if (btnExcluir.isVisible()) {
            btnExcluir.setEnabled(false);
        }
        btnAlterar.setEnabled(false);
        cmbPesquisa.setEnabled(false);
        mostrarCampos(false);
    }
    
    private boolean validar(String nome, String cpf, String cnpj, String endereco, String telefone) {
        boolean valido = true;

        if (nome.length() <= 3 || nome.length() > 40) {
            lblNome.setForeground(Color.red);
            valido = false;
        }

        if (cmbPessoa.getSelectedIndex() == 0) {
            //cnpj
            if (cnpj.length() == 14 && Util.isNumber(cnpj)) {
                cpf = "";
            } else {
                cmbPessoa.setForeground(Color.red);
                valido = false;
            }
        }

        if (cmbPessoa.getSelectedIndex() == 1) {
            //cpf
            if (cpf.length() == 11 && Util.isNumber(cpf)) {
                cnpj = "";
            } else {
                cmbPessoa.setForeground(Color.red);
                valido = false;
            }
        }

        if (endereco.length() <= 5 || endereco.length() > 40) {
            lblEndereco.setForeground(Color.red);
            valido = false;
        }

        if (telefone.isEmpty() || !Util.isNumber(telefone)) {
            lblTelefone.setForeground(Color.red);
            valido = false;
        }

        if (!valido) {
            JOptionPane.showMessageDialog(this, "Os campos com o nome em vermelho são inválidos.\nPor favor corrija.", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
        } 
        
        return valido;
    }
    
    private void mostrarCampos(boolean flag) {
        txtNome.setEnabled(flag);
        txtcpfCnpj.setEnabled(flag);
        txtEndereco.setEnabled(flag);
        txtTelefone.setEnabled(flag);
        txtEmail.setEnabled(flag);
        cmbPessoa.setEnabled(flag);
        cmbFornecedor.setEnabled(flag);
    }
    
    private void limparCampos() {
        lblCodigo.setText("--");
        txtNome.setText("");
        txtcpfCnpj.setText("");
        txtEndereco.setText("");
        txtTelefone.setText("");
        txtEmail.setText("");
        cmbPessoa.setSelectedIndex(1);
        cmbFornecedor.setSelectedIndex(0);
        
    }
    
    private void alterarLabels() {
        lblNome.setForeground(Color.black);
        lblCodigo.setForeground(Color.black);
        lblEndereco.setForeground(Color.black);
        lblTelefone.setForeground(Color.black);
        cmbPessoa.setForeground(Color.black);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel9 = new javax.swing.JLabel();
        cmbOpcaoPesquisa = new javax.swing.JComboBox();
        txtConsulta = new javax.swing.JTextField();
        btnPesquisa = new javax.swing.JToggleButton();
        btnConsultaTodos = new javax.swing.JButton();
        cmbPesquisa = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblCodigo = new javax.swing.JLabel();
        cmbPessoa = new javax.swing.JComboBox();
        txtcpfCnpj = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbFornecedor = new javax.swing.JComboBox();
        lblEndereco = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        lblTelefone = new javax.swing.JLabel();
        txtTelefone = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        btnNovo = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jLabel9.setText("Pesquisar por:");

        cmbOpcaoPesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nome", "CPF", "CNPJ", "e-mail" }));

        btnPesquisa.setText("Pesquisar");
        btnPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisaActionPerformed(evt);
            }
        });

        btnConsultaTodos.setText("Todos");
        btnConsultaTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultaTodosActionPerformed(evt);
            }
        });

        cmbPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbPesquisaActionPerformed(evt);
            }
        });

        jLabel2.setText("Código:");

        lblNome.setText("Nome:");

        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        lblCodigo.setText("--");

        cmbPessoa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CNPJ", "CPF" }));
        cmbPessoa.setSelectedIndex(1);

        txtcpfCnpj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcpfCnpjActionPerformed(evt);
            }
        });

        jLabel4.setText("Fornecedor:");

        cmbFornecedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Não", "Sim" }));
        cmbFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbFornecedorActionPerformed(evt);
            }
        });

        lblEndereco.setText("Endereço:");

        lblTelefone.setText("Telefone:");

        jLabel7.setText("e-mail:");

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAlterar.setText("Alterar");
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnExcluir.setText("Excluir");
        btnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("CADASTRO DE CLIENTES");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(lblTelefone)
                            .addComponent(jLabel2)
                            .addComponent(jLabel9)
                            .addComponent(lblNome)
                            .addComponent(cmbPessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblEndereco))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblCodigo)
                                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtcpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmbFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(txtEndereco)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmbOpcaoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPesquisa)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnConsultaTodos, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(184, 184, 184)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(btnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(66, 66, 66)
                                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cmbOpcaoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisa)
                    .addComponent(btnConsultaTodos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNome)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbPessoa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtcpfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cmbFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEndereco)
                    .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTelefone)
                    .addComponent(txtTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNovo)
                    .addComponent(btnSair)
                    .addComponent(btnAlterar))
                .addGap(18, 18, 18)
                .addComponent(btnExcluir)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisaActionPerformed
        if (lista != null) {
            lista.clear();
        }

        String filtro = "";
        if (txtConsulta.getText().length() > 0) {
            if (cmbOpcaoPesquisa.getSelectedIndex() == 0) {
                filtro = "nm_cliente LIKE " + "'" + txtConsulta.getText() + "%'";
            }

            if (cmbOpcaoPesquisa.getSelectedIndex() == 1) {
                filtro = "cd_cpf = " + "'" + txtConsulta.getText() + "'";
            }

            if (cmbOpcaoPesquisa.getSelectedIndex() == 2) {
                filtro = "cd_cnpj = " + "'" + txtConsulta.getText() + "'";
            }

            if (cmbOpcaoPesquisa.getSelectedIndex() == 3) {
                filtro = "nm_email LIKE " + "'" + txtConsulta.getText() + "%'";
            }

            try {
                lista = cliDao.consulta(filtro);

                if (lista.isEmpty()) {
                    JOptionPane.showMessageDialog(this, "Não foram localizados clientes: " + txtConsulta.getText(), "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                }

                if (lista.size() > 0) {
                    //limpa a lista
                    if (cmbPesquisa.getItemCount() > 0) {
                        cmbPesquisa.removeAllItems();
                    }
                    cmbPesquisa.setEnabled(true);
                    btnAlterar.setEnabled(true);
                    btnNovo.setText("Novo");
                    mostrarCampos(false);
                    alterarLabels();
                    if (btnExcluir.isVisible()) {
                        btnExcluir.setEnabled(true);
                    }
                    //seleciona o 1º (ou o unico) item da lista
                    ModeloCliente modeloCli = lista.get(0);
                    cmbPesquisa.addItem(modeloCli.getNome());
                    cmbPesquisa.setSelectedIndex(0);
                }

                if (lista.size() > 1) {
                    //apenas inclui os outros componentes na lista (se houver)
                    for (int i = 1; i <= lista.size() - 1; i++) {
                        ModeloCliente modeloCli = lista.get(i);
                        cmbPesquisa.addItem(modeloCli.getNome());
                    }
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Erro ao consultar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
            }

        }
    }//GEN-LAST:event_btnPesquisaActionPerformed

    private void btnConsultaTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultaTodosActionPerformed
        Cliente_ConsultaTodos cliModal = new Cliente_ConsultaTodos(mdi, true);
        cliModal.setVisible(true);

    }//GEN-LAST:event_btnConsultaTodosActionPerformed

    private void cmbPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPesquisaActionPerformed
        if (evt.getSource().getClass().getSimpleName().equals("JComboBox")) {
            JComboBox cmb = (JComboBox)evt.getSource();
            int index = cmb.getSelectedIndex();
            if (index != -1) {
                ModeloCliente mc = lista.get(index);
                String id = Integer.toString(mc.getId());
                String telefone = Integer.toString(mc.getTelefone());
                lblCodigo.setText(id);
                txtNome.setText(mc.getNome());
                txtEndereco.setText(mc.getEndereco());
                txtTelefone.setText(telefone);

                if (mc.getCnpj().isEmpty()) {
                    txtcpfCnpj.setText(mc.getCpf());
                    cmbPessoa.setSelectedIndex(1);
                } else {
                    txtcpfCnpj.setText(mc.getCnpj());
                    cmbPessoa.setSelectedIndex(0);
                }
                cmbFornecedor.setSelectedIndex(mc.getClienteFornecedor());
                txtEmail.setText(mc.getEmail());

            }
        }
    }//GEN-LAST:event_cmbPesquisaActionPerformed

    private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomeActionPerformed

    private void txtcpfCnpjActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcpfCnpjActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtcpfCnpjActionPerformed

    private void cmbFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbFornecedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbFornecedorActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        if (btnNovo.getText().equals("Novo")) {
            mostrarCampos(true);
            limparCampos();
            alterarLabels();
            btnNovo.setText("Salvar");
            btnAlterar.setEnabled(false);
            if (btnExcluir.isVisible()) {
                btnExcluir.setEnabled(false);
            }
        } else {
            //clicou no salvar
            String cpf = "";
            String cnpj = "";

            if (validar(txtNome.getText(), cpf, cnpj, txtEndereco.getText(), txtTelefone.getText())) {
                cli.setNome(txtNome.getText());
                cli.setEmail(txtEmail.getText());
                cli.setEndereco(txtEndereco.getText());
                cli.setTelefone(Integer.parseInt(txtTelefone.getText()));
                cli.setClienteFornecedor(cmbFornecedor.getSelectedIndex());
                cli.setCpf(cpf);
                cli.setCnpj(cnpj);

                try {
                    if (!cliDao.incluir(cli)) {
                        JOptionPane.showMessageDialog(this, "Dados gravados", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                        btnNovo.setText("Novo");
                        btnAlterar.setEnabled(true);
                        int maxId = cliDao.consultaMaxId();
                        lblCodigo.setText(Integer.toString(maxId));
                        mostrarCampos(false);
                        if (btnExcluir.isVisible()) {
                            btnExcluir.setEnabled(true);
                        }
                        alterarLabels();
                    }

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao gravar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (btnAlterar.getText().equals("Alterar")) {
            mostrarCampos(true);
            alterarLabels();
            btnAlterar.setText("Salvar");
            if (btnExcluir.isVisible()) {
                btnExcluir.setEnabled(false);
            }
        } else {
            //salvar
            String cpf = "";
            String cnpj = "";

            if (validar(txtNome.getText(), cpf, cnpj, txtEndereco.getText(), txtTelefone.getText())) {
                cli.setId(Integer.parseInt(lblCodigo.getText()));
                cli.setNome(txtNome.getText());
                cli.setEmail(txtEmail.getText());
                cli.setEndereco(txtEndereco.getText());
                cli.setTelefone(Integer.parseInt(txtTelefone.getText()));
                cli.setClienteFornecedor(cmbFornecedor.getSelectedIndex());
                cli.setCpf(cpf);
                cli.setCnpj(cnpj);

                try {
                    if (cliDao.alterar(cli) > 0) {
                        JOptionPane.showMessageDialog(this, "Dados alterados", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                        btnAlterar.setText("Alterar");
                        mostrarCampos(false);
                        alterarLabels();
                        if (btnExcluir.isVisible()) {
                            btnExcluir.setEnabled(true);
                        }
                    }

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Erro ao gravar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirActionPerformed
        if (lblCodigo.getText().equals("--")) {
            JOptionPane.showMessageDialog(this, "Registro inválido", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
            lblCodigo.setForeground(Color.red);
        } else {
            try {
                int id = Integer.parseInt(lblCodigo.getText());

                if (cliDao.deletar(id) == 1) {
                    JOptionPane.showMessageDialog(this, "Registro excluido", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
                    limparCampos();
                    mostrarCampos(false);
                    lblCodigo.setForeground(Color.black);
                    btnExcluir.setEnabled(false);
                    btnAlterar.setEnabled(false);
                }

            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Erro ao excluir " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnExcluirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnConsultaTodos;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JToggleButton btnPesquisa;
    private javax.swing.JButton btnSair;
    private javax.swing.JComboBox cmbFornecedor;
    private javax.swing.JComboBox cmbOpcaoPesquisa;
    private javax.swing.JComboBox cmbPesquisa;
    private javax.swing.JComboBox cmbPessoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblEndereco;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTelefone;
    private javax.swing.JTextField txtConsulta;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtTelefone;
    private javax.swing.JTextField txtcpfCnpj;
    // End of variables declaration//GEN-END:variables
}
